package com.csst.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;


import org.json.JSONObject;
import org.springframework.stereotype.Controller;

import com.csst.bean.User;
import com.csst.bean.UserBank;
import com.csst.service.UserBankService;
import com.csst.util.HttpUtil;
import com.csst.util.SignatureTool;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Controller
public class UserBankAction extends ActionSupport implements
ModelDriven<UserBank>, Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5749971069125730390L;
	
	@Resource
	private UserBankService service;
	private String payPassword;
	private UserBank bank;
	

	public UserBank getModel() {
		// TODO Auto-generated method stub
		return bank;
	}



	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		bank=new UserBank();
	}
	
	
	
	public String getPayPassword() {
		return payPassword;
	}
	public void setPayPassword(String payPassword) {
		this.payPassword = payPassword;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ActionContext context=ActionContext.getContext();
		SignatureTool tool=new SignatureTool();
		tool.setCheckCardParamter(bank.getCustomerName(), bank.getIdentityCardNum(), bank.getCardId(), 123456, payPassword, "jksas-jklwj-lwjjh-wmkhs-jklkj");
		String merSignMsg=tool.signature();
		Map<String, String> map=new HashMap<String, String>();
		map.put("customerName", bank.getCustomerName());
		map.put("identityCardNum", bank.getIdentityCardNum());
		map.put("bankCardNum", bank.getCardId());
		map.put("merID", "123456");
		map.put("merSignMsg", merSignMsg);
		String result=HttpUtil.doPost("http://bank:8080/obs/merchant/checkcard", map);
		JSONObject job=new JSONObject(result);
		System.out.println(job.get("result"));
		if(job.get("result").equals("PS"))
		{
			bank.setQuickPayKey((String)job.get("quickPayKey"));
			User user=(User)context.getSession().get("user");
			bank.setUser(user);
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = formatter.format(currentTime);
			bank.setBtime(dateString);
			service.saveBank(bank);
			return "success";
		}
		return "fail";
	}



	
	

}
